<?php
class sfFoxsisMenu extends sfWebRequest
{
    public $menu_yml;
    public $container;
    public $direction;
    private $config_menu = array();
    private $menus       = array();
    private $sf_user;

    public function __construct($sf_user, $menu, $container = 'menu', $direction = 'h', $theme = true)
    {
		$this->container = $container;
    	$this->menu_yml = $menu.'.yml';
    	$this->direction = $direction;
        $this->sf_user = $sf_user;
        $arquivoYml = sfConfig::get('sf_FoxsisMenu_yml') . $this->menu_yml;
        if(!file_exists($arquivoYml)) {
            trigger_error("Arquivo de configuração do menu '$arquivoYml' não encontrado");
        }
        $yf = sfYaml::load( sfConfig::get('sf_FoxsisMenu_yml') . $this->menu_yml);
       	$this->menus = $yf['menus'];

       	return $this->render();
    }

    public function setMenu($menu)
    {
    	$this->menu_yml = $menu;
    }

    public function render($theme = true)
    {
        $retorno = substr($this->criarMenus($this->menus), 0, -10);
        $html_menu = "<script type='text/javascript'>\n";
        $html_menu.= "var menu = [\n";
        $html_menu.= $retorno;
        $html_menu.= "]\n";
        if($theme)
            $html_menu.= "cmDraw ('{$this->container}', menu, '{$this->direction}br', cmThemeOffice, 'ThemeOffice');;\n";
        else
            $html_menu.= "cmDraw ('{$this->container}', menu, '{$this->direction}br');\n";
        $html_menu.= "</script>";

        echo $html_menu;
    }

    private function criarMenus($arr_menus, $parent = true)
    {
        $retorno = "";

        foreach ($arr_menus as $menu)
        {
            if($parent === false)
                $retorno .= "\t";

            $retorno.= "[";
            if($this->getArrayValue($menu, 'image', false) != "")
                $retorno.= "'". image_tag('menu/'.$this->getArrayValue($menu, 'image', false)) ."',";
            else
                $retorno.= $this->getArrayValue($menu, 'image') . ",";

            // nome
            $retorno.= $this->getArrayValue($menu, 'text') .",";

            // url
            $url = "";
            $module = $this->getArrayValue($menu, 'module', false);
            $action = $this->getArrayValue($menu, 'action', false);
        	if ($module || $action){
            	if($action == "")
                	$url = url_for("$module");
                else
                	$url = url_for("$module/$action");
            }

            $retorno.= "'". $url ."',";

            // target
            $retorno.= $this->getArrayValue($menu, 'target') .",";
            // descricao
            $retorno.= $this->getArrayValue($menu, 'description');

            if(isset($menu['submenus'])) $retorno .= ",\n".$this->criarMenus($menu['submenus'], false);

            $retorno.= "],\n";

            if($parent === true)
                    $retorno.= "_cmSplit,\n";
        }

        return $retorno;
    }

    private function getArrayValue($array, $key, $addAspas = true)
    {
        $result = '';
        if (isset($array[$key])){
            $result = $array[$key];
        }

        return $addAspas ? $this->addAspas($result) : $result;
    }

    private function addAspas($string)
    {
        if ($string === ''){
            return "''";
        } else {
            return is_numeric($string) ? $string : "'$string'";
        }
    }
}